package com.yunji.cabinet.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yunji.cabinet.bo.CabinetCellListQueryBO;
import com.yunji.cabinet.bo.TemplateCellResetBO;
import com.yunji.cabinet.dao.ICabinetTemplateCellDao;
import com.yunji.cabinet.domain.CabinetTemplateDO;
import com.yunji.cabinet.domain.CabinetTemplateCellDO;
import com.yunji.cabinet.mapper.CabinetTemplateCellMapper;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.constant.LogicCode;
import com.yunji.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CabinetTemplateCellDaoImpl extends BaseDaoImpl<CabinetTemplateCellMapper, CabinetTemplateCellDO> implements ICabinetTemplateCellDao {
    @Override
    public int resetCell(TemplateCellResetBO cell) {
        return baseMapper.resetCell(cell);
    }

    @Override
    public List<CabinetTemplateCellDO> searchTemplateCells(CabinetCellListQueryBO bo) {
        MPJLambdaWrapper<CabinetTemplateCellDO> wrapper = new MPJLambdaWrapper<>();
        return selectJoinList(CabinetTemplateCellDO.class, wrapper.selectAll(CabinetTemplateCellDO.class)
                .leftJoin(CabinetTemplateDO.class, CabinetTemplateDO::getTemplateId, CabinetTemplateCellDO::getTemplateId)
                .eq(CabinetTemplateCellDO::getStatus, LogicCode.NO)
                .eq(CabinetTemplateDO::getStatus, LogicCode.NO)
                .eq(BeanUtils.isNotNull(bo.getBelongId()), CabinetTemplateDO::getBelongId, bo.getBelongId())
                .eq(BeanUtils.isNotNull(bo.getProductId()), CabinetTemplateCellDO::getProductId, bo.getProductId())
                .eq(BeanUtils.isNotNull(bo.getTemplateId()), CabinetTemplateDO::getTemplateId, bo.getTemplateId()));
    }

    @Override
    public boolean deleteByTemplateIds(List<Long> list) {
        return remove(new LambdaQueryWrapper<CabinetTemplateCellDO>()
                .in(CabinetTemplateCellDO::getTemplateId,list));
    }
}
